<template>
	<view class="ljapp">
		<view class="content">
			<view class="report-file u-flex u-flex-y-center" v-for="(item,index) in group.list" :key="index">
				<view class="inner u-flex-fill u-flex u-flex-y-center" @click="onSelect(item)">
					<image src="/static/lj/common/group_712.png" mode="aspectFill"></image>
					<view>
						<view class="tit">{{obj.name}}</view>
						<view class="time">{{item.reportDataTime}}</view>
					</view>
				</view>
			</view>
			
			<lj-empty v-if="group.loaded&&group.list.length<=0">暂无报告</lj-empty>
			<view v-else>
				<u-loadmore
					:status="loading?'loading':(group.loaded?'nomore':'loadmore')"
					fontSize="28rpx"
					color="#d9d9d9"
					nomoreText="已加载全部"
					marginTop="30rpx"
					marginBottom="0"
					@loadmore="getList"
				></u-loadmore>
			</view>
		</view>
		
		<u-popup :show="showFilePop" :round="10" mode="center">
			<view class="popup-empty-file-content">
				<image class="close" src="/static/lj/drug/icon_close.png" mode="aspectFill" @click="showFilePop=false"></image>
				<view class="popup-title">暂无检测报告，是否前去检测？</view>
				<view class="popup-content u-flex u-flex-between">
					<view class="item">
						<view class="btn-fillet" @click="gotoMini(pwappid, pwpage)">去检测</view>
						<view class="tip">前往维普小程序检测</view>
					</view>
					<view class="item">
						<view class="btn-fillet-o" @click="uploadReport">上传报告</view>
						<view class="tip">已有其他机构检测报告</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pwappid: '',
				pwpage: '',
				jkscappid: '',
				jkscpage: '',
				
				showFilePop: false,
				
				id_card: '',
				obj: {},
				group:{
					page: 1,
					limit: 10,
					list: [],
					loaded: false,
				},
				loading: false,
			}
		},
		onLoad(options) {
			var that = this
			if(options.id_card) that.id_card = options.id_card
			
			that.$Http({
				url: 'getOthMiniConfig',
				data: {},
				token: false
			}).then(res => {
				if (res.code == 1) {
					that.pwappid = res.data.pwappid
					that.pwpage = res.data.pwpage
					that.jkscappid = res.data.jkscappid
					that.jkscpage = res.data.jkscpage
				}
			});
			
			this.init()
		},
		onShow(){
			// this.init()
		},
		methods: {
			gotoMini(appid, page){
				uni.navigateToMiniProgram({
					appId: appid,
					path: page,
					success(e) {
						console.log('gotoMini', e)
					},
					fail(e) {
						console.log('gotoMini fail',e)
					}
				})
			},
			init(){
				this.group.page = 1
				this.group.list = []
				this.group.loaded = false
				this.loading = false
			
				this.getList();
			},
			getList(){
				var that = this, group = that.group;
				if(that.loading || group.loaded) return;
			
				that.loading = true
			
				this.$Http({
					url: 'getReportSelect2',
					data: {
						page: group.page,
						limit: group.limit,
						id_card: this.id_card,
					},
					type:'POST',
				}).then(res=>{
					if(res.code == 1){
						that.obj = res.data.data
						var list = res.data.list;
						group.loaded = true;
						group.page ++;
						group.list = group.list.concat(list);
						
						if(group.list.length<=0) that.showFilePop = true
					}else{
						
					}
					
					that.loading = false
				}).catch(()=>{
					that.loading = false
				});
			},
			onReachBottom(){
				this.getList()
			},
			onSelect(item){
				console.log('onSelect',item)
				var obj = this.obj
				obj.report_code = item.reportCode
				obj.report_filename = item.reportName
				obj.report_time = item.reportDataTime
				obj.report_url = item.reportUrl

				this.$store.commit('selectReportobj', obj)
				
				uni.navigateBack({})
			},
			
			uploadReport(){
				var that = this
				wx.chooseMessageFile({
					count: 1,
					type: 'file',
					extension: ['pdf'],
					success(res){
						console.log(res)
						var tempFiles = res.tempFiles
						if(!tempFiles || tempFiles.length<=0){
							that.$u.toast("选取文件失败.")
							return false
						}
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						uni.uploadFile({
							url: that.$Config.http_static + 'api/common/uploader',
							filePath: tempFiles[0].path,
							name: 'file',
							formData: {},
							success: (uploadFileRes) => {
								uni.hideLoading()
								console.log('uploadReport uploadFile',uploadFileRes)
								var obj = JSON.parse(uploadFileRes.data);
								console.log('uploadReport uploadFile',obj)
								if(obj.msg = "上传成功"){
									that.$store.commit('selectReportobj', {
										name: '',
										age: 0, //年龄
										sex: 1, //性别1-男,2-女
										mobile: '', //手机号
										id_card: that.id_card, //身份证号
										
										report_code: '',
										report_filename: tempFiles[0].name,
										report_time: that.$u.timeFormat(tempFiles[0].time,'yyyy-mm-dd hh:MM'),
										report_url: obj.data.fullurl,
									})
									
									uni.navigateBack({})
								}else{
									that.$u.toast('上传失败');
								}
							},
							fail(e) {
								console.log('uploadReport uploadFile fail',e)
								uni.hideLoading()
								that.$u.toast('上传失败');
							}
						})
					},
					fail(e){
						console.log("chooseMessageFile fail ", e);
						that.$u.toast("选取文件失败")
					}
				})
			},
		}
	}
</script>

<style lang="less">
	@import '@/pagesL/style.less';
	
	.content{
		padding: 20rpx;
	}
	.report-file{
		.inner{
			border: none;
			margin-bottom: 20rpx;
			box-shadow: 0 4rpx 6rpx #eee;
		}
	}
	
	.popup-empty-file-content{
		position: relative;
		width: 600rpx;
		padding: 74rpx 0 44rpx;
		.close{
			position: absolute;
			top: 24rpx;
			right: 30rpx;
			width: 30rpx;
			height: 30rpx;;
		}
		
		.popup-title{
			text-align: center;
			font-size: 30rpx;
			color: #333333;
		}
		.popup-content{
			text-align: center;
			padding: 0 40rpx;
			margin-top: 50rpx;
			.tip{
				font-size: 22rpx;
				color: #999999;
				margin-top: 12rpx;
			}
		}
		
		.btn-fillet,.btn-fillet-o{
			width: 240rpx;
			height: 84rpx;
			line-height: 80rpx;
			text-align: center;
			font-size: 30rpx;
			color: #65d8f2;
			border-radius: 10rpx;
			border: 2rpx solid #65d8f2;
		}
		.btn-fillet{
			background-color: #12b3f1;
			color: #fff;
			border-color: #12b3f1;
		}
	}
</style>
